from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.core.paginator import Paginator
from cassacoproject.cassaco.models import Spent
from cassacoproject.cassaco.models import Category
from cassacoproject.cassaco.models import Subcategory
from cassacoproject.cassaco.models import Mode
import util
import chart

PAGINATOR = 15

def _configurePaginator(page, spents):
    paginator = Paginator(spents, PAGINATOR)
    beginPageList = 0
    endPageList = 10
    
    if abs(int(page) - endPageList) >= 0:
        endPageList = int(page) + 3
        
    return {
               'spents': paginator.page(page).object_list,
               'paginator': paginator.page_range[beginPageList:endPageList],
               'page': int(page),
               'hasNext': paginator.page(page).has_next(),
               'hasPrevious': paginator.page(page).has_previous()
           }

@login_required
def list(request, page=1):
	# list by user
    spents = Spent.objects.filter(user=request.user).order_by('-date')
    catsAndSubs = util.getCategoriesAndSubcategoriesDict()
    
    paginatorParams = _configurePaginator(page, spents)
    params = {'catsAndSubs': catsAndSubs,
                   'pieChart': chart.getPieChartUrl(spents),
                   'linkPaginator': '/cassaco/spent/list/'}
    params.update(paginatorParams)
    
    return render_to_response('spent/list.html', params)

@login_required
def listByDate(request, page, date):
    spents = Spent.objects.filter(user=request.user, 
                                  date=date).order_by('-date')
    catsAndSubs = util.getCategoriesAndSubcategoriesDict()
    params = {
               'catsAndSubs': catsAndSubs,
               'pieChart': chart.getPieChartUrl(spents),
               'date': '/'+date,
               'linkPaginator': '/cassaco/spent/list/'
               }
    params.update(_configurePaginator(page, spents))
    return render_to_response('spent/list.html', params)

@login_required
def listBetweenDate(request, page, date, date2):
    spents = Spent.objects.filter(date__gte=date, 
                                  date__lte=date2,
                                  user=request.user).order_by('-date')
    catsAndSubs = util.getCategoriesAndSubcategoriesDict()
    params = {
               'catsAndSubs': catsAndSubs,
               'pieChart': chart.getPieChartUrl(spents),
               'date': '/'+date,
               'date2': '/'+date2,
               'linkPaginator': '/cassaco/spent/list/'
               }
    params.update(_configurePaginator(page, spents))
    return render_to_response('spent/list.html', params)
    
@login_required
def save(request):
    newSpent = Spent()
    
    if request.POST['id']:
        newSpent = Spent.objects.get(id=request.POST['id'],
                                     user=request.user)
        
        if request.POST.has_key('delete') and request.POST['delete'] == 'true':
            newSpent.delete()
            return HttpResponseRedirect(
                    reverse('cassacoproject.cassaco.views.index'))
        
    newSpent.description = request.POST['description']
    newSpent.category = get_object_or_404(Category, 
                              pk=request.POST['category_id'])
    newSpent.subcategory = get_object_or_404(Subcategory, 
                              pk=request.POST['subcategory_id'])
    newSpent.mode = get_object_or_404(Mode, 
                              pk=request.POST['mode_id'])
    newSpent.date = request.POST['date']
    newSpent.value = request.POST['value']
    newSpent.user = request.user
    newSpent.save()
    
    return HttpResponseRedirect('/cassaco')

@login_required
def delete(request):
    ids = request.POST.getlist('id')
    for id in ids:
        Spent.objects.get(id=int(id), user=request.user).delete()
        
    return HttpResponseRedirect('/cassaco')

@login_required
def edit(request, id):
    spent = Spent.objects.get(id=id, user=request.user)
    return render_to_response('spent/spentForm.html', {'spent': spent})

@login_required
def new(request):
    return render_to_response('spent/spentForm.html')

@login_required
def listByCategory(request, category_id, page):
    spents = Spent.objects.filter(category=category_id, user=request.user)
    params = {
               'pieChart': chart.getSubPieChartUrl(spents, category_id),
               'linkPaginator': '/cassaco/spent/listByCategory/%s/'%category_id
               }
    params.update(_configurePaginator(page, spents))
    return render_to_response('spent/list.html', params)

@login_required
def listBySubcategory(request, subcategory_id, page):
    spents = Spent.objects.filter(subcategory=subcategory_id, user=request.user)
    params = {
               'linkPaginator': '/cassaco/spent/listBySubcategory/%s/'%subcategory_id
               }
    params.update(_configurePaginator(page, spents))
    return render_to_response('spent/list.html',params)